#include <iostream>
#include <vector>
#include <stats.h>

#include <stdio.h>
#include <stdlib.h>

#include <player.h>

#ifndef _GLOBALS_H_
#define _GLOBALS_H_

/**
 * Cerrano I/O Stuff ( how it looks )
 *
 * name:foo
 * key1:value
 * key2:value
 * key3:value
 * .
 * name:foo
 * key1:value
 * key2:value
 * key3:value
 * .
 * name:foo
 * key1:value
 * key2:value
 * key3:value
 * .
 * name:foo
 * key1:value
 * key2:value
 * key3:value
 * .
 * name:foo
 * key1:value
 * key2:value
 * key3:value
 * .
 * .
 *
 * We'll end it when there's a new statset without a name
 * after all, we can't have birdman yell out names in the
 * anouncer voice without a name. Every "block" of keypairs
 * between dots is a statset, just FYI.
 * 
 * This also makes it really easy to code for, and I am
 * ( after all ) lazy.
 * 
 * Muchos Grande,
 * PRT
 * 
 */

/**
 * I'm going to use this under the hood to throw
 * attr errors and such. I used this because it's
 * super easy to remember. Much love. PRT
 */
class BadbeefException : public std::exception {
	virtual const char* what() const throw() {
		return "f7u12";
	}
} _badbeef; // use internally to not start leaking memory

#define attr_error BadbeefException /* let's make it "look" nice */

/**
 * Format messages in header format to stdout
 */
void output_msg( std::string note ) {
	std::cout << "msg:" << note << std::endl;
}

void output_dbug( std::string note ) {
#ifdef DEBUG
	std::cout << "debug:" << note << std::endl;
#endif
}

void clean_list( std::vector<player *> * list ) {
	for ( unsigned int i = 0; i < list->size(); ++i ) {
		try {
			std::string trash = list->at(i)->getStat("name")->getValue();
			if ( trash == "" )
				throw _badbeef;
			output_dbug( " -> good" );

		} catch ( attr_error& ) {
			output_dbug( "Removing a malformated entry" );
			list->erase(list->begin()+i);
			i = 0;
			output_dbug( " Starting processing over." );
		}
	}
	output_dbug( "Cleaned list." );
}

/**
 * print out the READY message to the stdout
 */
void header() {
	output_msg( "online" );
}

/**
 * Get the "ID" from the "header" string
 * @return the ID half of the header
 */
std::string get_id( std::string line ) {
	int index = line.find_first_of( ":" );

	if ( index < 0 )
		return "";

	return line.substr( 0, index );
}

/**
 * Get the "data" from a "header" string
 * @return the data half of a header
 */
std::string get_data( std::string line ) {
	int index = line.find_first_of( ":" );

	if ( index < 0 )
		return "";

	return line.substr( index+1, line.size() );
}

/**
 * Load in a statistic set from stdin
 * @return a std::vector<stats *> * to the data inside the function
 */
std::vector<stats *> *  load_data() {
	std::string line = "";

	std::vector<stats *> * ret = new std::vector<stats *>();

	while ( getline( std::cin, line ) ) {
		if ( line == "." ) {
			output_dbug( "Stopping here." );
			break;
		} else {
			std::string id   = get_id(   line );
			std::string data = get_data( line );
			if ( id == "" || data == "" ) {
				// whine!
				output_msg( "Dude, stop giving me bad data." );
			} else {
				stats * s = new stats();
				s->setType("String");
				s->setValue( data );
				s->setName( id );
				ret->push_back( s );
			}
		}
	}

	return ret;
}

#endif

